package com.iot.nev.gateway.connector.mongdb;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

import org.bson.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

public class SaveDataForMongoDB {
	private static Logger logger = LoggerFactory.getLogger(SaveDataForMongoDB.class); 
	
	public static void saveData(String message){
		int a1[] = {3, 5, 1, 5466, 9999, 2645, 6355, 12, 13, 14, 42345, 15, 16};
    	int a2[] = {2, 12, 3, 210, 32345, 24567, 187, 45678, 284955, 2, 227, 54789, 24578, 168, 54678, 192};
    	int a3[] = {7000, 3112, 32132, 22442, 151, 11, 10450, 23, 11223, 52, 52};
    	int a4[] = {16, 100, 3800};
    	int a5[] = {5, 5, 5};
    	int a6[] = {44, 45, 89, 58, 59, 151, 26, 20, 97, 19, 4, 15};
    	int a7[] = {2, 65535, 7, 7, 7, 7};
    	int a8[] = {1 ,1 ,8888, 88, 21, 54, 78};
    	int a9[] = {1, 221, 55871};
    	MongoDBStorage mongodb = MongoDBStorage.getInstance();
    	MongoClient mongoClient = mongodb.setUp();
    	try {
			// 连接到数据库
			MongoDatabase mongoDatabase = mongoClient.getDatabase("car");
			MongoCollection<Document> collection = mongoDatabase.getCollection("battery");
			String tm = new SimpleDateFormat("yyyy/MM/dd-HH:mm:ss:SSS").format(new Date());
			Vector<Document> vec = new Vector<Document>();
			Document document1 = new Document();
			document1.append("time", tm);
			document1.append("0x01", "0x01");
			document1.append("bVehicle_Status1", a1[0]);
			document1.append("bCharge_Status1", a1[1]);
			document1.append("bRunning_Mode1", a1[2]);
			document1.append("wVehicle_Speed1", a1[3]);
			document1.append("wTotal_Voltage1", a1[4]);
			document1.append("wTotal_Current1", a1[5]);
			document1.append("bSOC1", a1[6]);
			document1.append("bDCDC_Status1", a1[7]);
			document1.append("bGear1", a1[8]);
			document1.append("wResPositive1", a1[9]);
			document1.append("bAccPedal1", a1[10]);
			document1.append("bBrkPedal1", a1[11]);
			vec.add(document1);
			Document document2 = new Document();
			document2.append("0x02", "0x02");
			document2.append("bMotor_Num2", a2[0]);
			document2.append("bMotor_NO2", a2[1]);
			document2.append("bMotor_Status2", a2[2]);
			document2.append("bMotor_Controller_Temperature2", a2[3]);
			document2.append("wMotor_Speed2", a2[4]);
			document2.append("wMotor_Torque2", a2[5]);
			document2.append("bMotor_Temperature2", a2[6]);
			document2.append("wMotor_Voltage2", a2[7]);
			document2.append("wMotor_Current2", a2[8]);
			document2.append("bMotor_NO22", a2[9]);
			document2.append("bMotor_Status22", a2[10]);
			document2.append("bMotor_Controller_Temperature22", a2[11]);
			document2.append("wMotor_Speed22", a2[12]);
			document2.append("wMotor_Torque22", a2[13]);
			document2.append("bMotor_Temperature22", a2[14]);
			document2.append("wMotor_Voltage22", a2[15]);
			vec.add(document2);
			Document document3 = new Document();
			document3.append("0x03", "0x03");
			document3.append("wBattery_Voltage3", a3[0]);
			document3.append("wBattery_Current3", a3[1]);
			document3.append("wFuel_Cnsumption_Rate3", a3[2]);
			document3.append("wTemperature_Probes_Num3", a3[3]);
			document3.append("wMax_Temperature3", a3[4]);
			document3.append("bMax_Temperature_Probe_No3", a3[5]);
			document3.append("wMax_Hydrogen_Concentration3", a3[6]);
			document3.append("bMax_Hydrogen_Concentration_No3", a3[7]);
			document3.append("wMax_Hydrogen_Pressure3", a3[8]);
			document3.append("bMax_Hydrogen_Pressure_No3", a3[9]);
			document3.append("bHigh_Voltage_DCDC_STT3", a3[10]);
			vec.add(document3);
			Document document4 = new Document();
			document4.append("0x04", "0x04");
			document4.append("bEngine_Status4", a4[0]);
			document4.append("wCrankshaft_Speed4", a4[1]);
			document4.append("wFuel_Consumption_Rate4", a4[2]);
			vec.add(document4);
			Document document5 = new Document();
			document5.append("0x05", "0x05");
			document5.append("bGPS_Status5", a5[0]);
			document5.append("dLatitude5", a5[1]);
			document5.append("dLongitude5", a5[2]);
			vec.add(document5);
			Document document6 = new Document();
			document6.append("0x06", "0x06");
			document6.append("bBattery_Subsystem_Max_Voltage_No6", a6[0]);
			document6.append("bBattery_Cell_Max_Voltage_No6", a6[1]);
			document6.append("wBattery_Cell_Max_Voltage6", a6[2]);
			document6.append("bBattery_Subsystem_Min_Voltage_No6", a6[3]);
			document6.append("bBattery_Cell_Min_Voltage_No6", a6[4]);
			document6.append("wBattery_Cell_Min_Voltage6", a6[5]);
			document6.append("bBattery_Subsystem_Max_T_No6", a6[6]);
			document6.append("bProbe_Max_T_No6", a6[7]);
			document6.append("wProbe_Max_T6", a6[8]);
			document6.append("bBattery_Subsystem_Min_T_No6", a6[9]);
			document6.append("bProbe_Min_T_No6", a6[10]);
			document6.append("wProbe_Min_T6", a6[11]);
			vec.add(document6);
			Document document7 = new Document();
			document7.append("0x07", "0x07");
			document7.append("bHighest_Alarm_Level7", a7[0]);
			document7.append("dGeneral_Alarm_Flag7", a7[1]);
			document7.append("bBattery_Error_Num7", a7[2]);
			document7.append("bMotor_Error_Num7", a7[3]);
			document7.append("bEngine_Error_Num7", a7[4]);
			document7.append("bOther_Error_Num7", a7[5]);
			vec.add(document7);
			Document document8 = new Document();
			document8.append("0x08", "0x08");
			document8.append("bEnergyStorageSubsystemsNum8", a8[0]);
			document8.append("bEnergyStorageSubsystemsNo8", a8[1]);
			document8.append("wVoltage8", a8[2]);
			document8.append("wCurrent8", a8[3]);
			document8.append("wCellNum8", a8[4]);
			document8.append("wStart_Cell_Id8", a8[5]);
			document8.append("bCurrent_Cell_Num8", a8[6]);
			vec.add(document8);
			Document document9 = new Document();
			document9.append("0x09", "0x09");
			document9.append("bEnergyStorageSubsystemsNum9", a9[0]);
			document9.append("bEnergyStorageSubsystemsNo9", a9[1]);
			document9.append("wTemperature_Probes_Num9", a9[2]);
			vec.add(document9);
			Document document = new Document();
			document.append("category", vec);
			collection.insertOne(document);
		} catch (Exception e) {
			System.out.println(e.getClass().getName() + ": " + e.getMessage());
		}
	}

}
